﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace B13_Ex02_David_300512340_Alexey_321203689
{
    public partial class AlbumForm : Form
    {
        private IAlbumLogic AlbumFormLogicProxy;
        private List<Image> m_ImagesList = new List<Image>(); // The slide show take the images from this list.
        private int m_Index = 0; // Index for the listBoxPictures, used when click on buttonSlideShow. 

        public AlbumForm()
        {
            InitializeComponent();
        }

        public AlbumForm(AlbumWrapper i_SelectedAlbum)
        {
            InitializeComponent();
            AlbumFormLogicProxy = new AlbumFormLogicProxy(i_SelectedAlbum);
            listBoxPictures.DataSource = i_SelectedAlbum.GetAlbumPhotosList();
        }

        private void listBoxPictures_SelectedIndexChanged(object sender, EventArgs e)
        {
            PhotoWrapper selectedPhoto;

            if (listBoxPictures.SelectedItems.Count == 1)
            {
                selectedPhoto = listBoxPictures.SelectedItem as PhotoWrapper;
                pictureBoxFromAlbum.LoadAsync(selectedPhoto.GetPhotoURL);
            }
        }

        private void buttonSaveSelectedPhoto_Click(object sender, EventArgs e)
        {
            AlbumFormLogicProxy.SaveSelectedPhotos(getPath(), textBoxPictueSaveName.Text, listBoxPictures);
        }

        private void buttonSaveAlbum_Click(object sender, EventArgs e)
        {
            AlbumFormLogicProxy.SaveAlbum((AlbumFormLogicProxy as AlbumFormLogicProxy).SelectedAlbum, getPath(), textBoxPictueSaveName.Text);
        }

        private string getPath()
        {
            folderBrowserDialog.ShowDialog();

            return folderBrowserDialog.SelectedPath;
        }

        private void listBoxPictures_MouseUp(object sender, MouseEventArgs e)
        {
            int point = listBoxPictures.IndexFromPoint(e.X, e.Y);
            PhotoWrapper item = point >= 0 ? listBoxPictures.Items[point] as PhotoWrapper : null; // If the album is empty, null.
            PhotoWrapper photo = null;

            if (listBoxPictures.SelectedItems.Contains(item))
            {
                photo = item;
                pictureBoxFromAlbum.LoadAsync(photo.GetPhotoURL);
            }
        }

        private void buttonSlideShow_Click(object sender, EventArgs e)
        {
            int timeSlide;

            // Checks if the album is loaded even before.
            if (m_ImagesList.Count == 0)
            {
                m_ImagesList = AlbumFormLogicProxy.PhotosPreLoader((AlbumFormLogicProxy as AlbumFormLogicProxy).SelectedAlbum.GetAlbumPhotosList());
            }

            if (listBoxPictures.Items.Count > 1)
            {
                try
                {
                    timeSlide = int.Parse(comboBoxSlideTime.Text) * 1000;
                }
                catch
                {
                    MessageBox.Show("Invalid input. Default interval 2 seconds will be used.", "Cant play", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    comboBoxSlideTime.Text = "2";
                    timeSlide = 2000;
                }

                timerSlideShow.Interval = timeSlide;
                timerSlideShow.Enabled = !timerSlideShow.Enabled;
                buttonSlideShow.Text = buttonSlideShow.Text == "Play slide show" ? "Stop slide show" : "Play slide show";
                buttonResetSlideShow.Enabled = !buttonResetSlideShow.Enabled;
            }
            else
            {
                MessageBox.Show("Only one picture in this album.", "Cant play", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
        }

        private void timerTick(object sender, EventArgs e)
        {
            PhotoWrapper photo;

            if (m_Index < listBoxPictures.Items.Count)
            {
                photo = listBoxPictures.Items[m_Index] as PhotoWrapper;
                pictureBoxFromAlbum.Image = m_ImagesList[m_Index];
                listBoxPictures.ClearSelected();
                listBoxPictures.SelectedIndex = m_Index;
                m_Index++;
            }
            else
            {
                m_Index = 0;
                timerSlideShow.Stop();
                buttonSlideShow.Text = buttonSlideShow.Text == "Play slide show" ? "Stop slide show" : "Play slide show";
                buttonResetSlideShow.Enabled = !buttonResetSlideShow.Enabled;
            }
        }

        private void buttonResetSlideShow_Click(object sender, EventArgs e)
        {
            timerSlideShow.Stop();
            m_Index = 0;
            listBoxPictures.ClearSelected();
            listBoxPictures.SelectedIndex = m_Index;
        }
    }
}
